package net.cyndeline.GUI.graphics

import net.cyndeline.GUI.PaintManager
import net.cyndeline.game.defaultImpl.ProcessCycle
import net.cyndeline.GUI.MapModifier
import javafx.util.Duration
import javafx.event.EventHandler
import javafx.event.ActionEvent
import javafx.animation.KeyFrame
import javafx.animation.TimelineBuilder
import javafx.animation.Animation

/**
 * Re-paints the map an infinite number of times. No message is
 * required to stop the manager, since it never breaks out of
 * the paint loop. Instead, stop the actor and restart it.
 */
class GraphicsPainter extends PaintManager {
  
  override def act() {
    loop {
      react {
        case MapToPaint(map) => {
          /* Start the painting. */
          val duration: Duration = Duration.millis(1000 / 60)
	      val handler = new EventHandler[ActionEvent]() {
        	override def handle(event: ActionEvent) {
        	  map.paint
	        }
	      }
          
	      val frame: KeyFrame = new KeyFrame(duration, handler)
	      
	      TimelineBuilder.create().cycleCount(Animation.INDEFINITE)
	      	.keyFrames(frame)
	      	.build()
	      	.play()
        }
        
        case _ => {}
      }
    }
  }
}

case class MapToPaint(map: MapModifier)